package cn.lixufeng.basic.service.impl;


import cn.lixufeng.basic.mapper.BaseMapper;
import cn.lixufeng.basic.query.BaseQuery;
import cn.lixufeng.basic.service.IBaseService;
import cn.lixufeng.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 这里不能加service注解
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired  // 用泛型注入
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void add(T t) {
        mapper.add(t);
    }

    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public T findById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    /**
     * 查询分页数据
     * @param query
     * @return
     */
    @Override
    public PageList<T> findPage(BaseQuery query) {
        Integer total = mapper.findCount(query);
        List<T> rows = mapper.findPageData(query);
        return new PageList<>(total,rows);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void batchDelete(List<Long> ids) {
        mapper.batchDelete(ids);
    }
}
